package cn.icare.cms.controller.restful;

import cn.icare.cms.ItemStoreService;
import cn.icare.cms.model.ro.AddItemStoreRO;
import cn.icare.cms.model.ro.EditItemStoreRO;
import cn.icare.cms.model.ro.SaveItemStoreRO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/itemstore")
@Api(tags = "REST商品库存")
public class ItemStoreController extends BaseController {
    @Resource
    private ItemStoreService itemStoreService;
    private final String FALL_STRING = "请求失败,服务繁忙!";

    /**
     * URL:http://{ip}:{port}/rest/itemstore
     * method:POST
     *
     * @param ro
     * @return
     */
    @PostMapping()
    @HystrixCommand(fallbackMethod = "createOneFall")

    @ApiOperation(value = "保存商品库存", notes = "保存商品库存")
    public ResponseEntity<RespMsgVO> saveOne(@RequestBody AddItemStoreRO ro, HttpServletRequest request) {

        String userName=getCurrentUserName(request);
        Long id = this.itemStoreService.createOne(ro,userName);
        if (id >= 0) {
            return super.getRespOk(id);
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/itemstore
     * method:PUT
     *
     * @param ro
     * @return
     */
    @PutMapping()
    @HystrixCommand(fallbackMethod = "updateOneFall")
    @ApiOperation(value = "更新单个仓库", notes = "更新单个仓库")
    public ResponseEntity<RespMsgVO> updateOne(@RequestBody EditItemStoreRO ro,HttpServletRequest request) {

        if (this.itemStoreService.updateOne(ro)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/itemstore/{id}
     * method:DELETE
     *
     * @param itemId
     * @param storeId
     * @return
     */
    @DeleteMapping("/{id}")
    @HystrixCommand(fallbackMethod = "deleteOneFall")
    @ApiOperation(value = "删除单个仓库", notes = "删除符合主键的单个仓库")
    public ResponseEntity<RespMsgVO> deleteOne(@PathVariable("itemId") Long itemId,@PathVariable("storeId") Integer storeId) {
        if (this.itemStoreService.deleteOne(itemId,storeId)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }



    /**
     * URL:http://{ip}:{port}/rest/itemstore?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @HystrixCommand(fallbackMethod = "getListFall")
    @ApiOperation(value = "查询商品库存列表", notes = "查询商品库存列表")
    public ResponseEntity<RespMsgVO> getList(@RequestParam(name = "itemId", required = false) Long itemId

    ) {
        return super.getRespOk(this.itemStoreService.getListByItem(itemId));
    }

    /**
     * URL:http://{ip}:{port}/rest/itemstore?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping("/getWaitEditNum")
    @HystrixCommand(fallbackMethod = "getWaitEditNumFall")
    @ApiOperation(value = "获取商品待设置库存的仓库列表", notes = "获取商品待设置库存的仓库列表，包含库存数为0的仓库")
    public ResponseEntity<RespMsgVO> getWaitEditNum(@RequestParam(name = "itemId", required = false) Long itemId,
                                                    @RequestParam(name = "areaId", required = false) Integer areaId

    ) {
        return super.getRespOk(this.itemStoreService.getWaitEditNum(itemId,areaId));
    }



    //#region fallback methods
    private ResponseEntity<RespMsgVO> createOneFall(AddItemStoreRO ro,HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> updateOneFall(EditItemStoreRO ro,HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteOneFall(Long itemId,Integer storeId) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getListFall(Long itemId) {
        return super.getRespFail(this.FALL_STRING);
    }
    private ResponseEntity<RespMsgVO> getWaitEditNumFall(Long itemId,Integer areaId) {
        return super.getRespFail(this.FALL_STRING);
    }


    //#endregion

}
